package com.hanson.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanson.common.bean.GenerateBean;
import com.hanson.common.bean.ResultMessage;
import com.hanson.common.service.RedisService;
import com.hanson.common.util.BusinessUtil;
import com.hanson.common.util.PageInfo;
import com.hanson.entity.*;
import com.hanson.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/admin/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsTypeService goodsTypeService;
    @Autowired
    private GoodsRuleService goodsRuleService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private GoodsScoreService goodsScoreService;
    @Autowired
    private RedisService redisService;

    @RequestMapping(value = "/setScore", method = RequestMethod.GET)
    public String setScore(ModelMap modelMap, String goodsId) {
        GoodsScoreBean goodsScoreBean = goodsScoreService.getGoodsScore(Integer.parseInt(goodsId));
        modelMap.put("data",null == goodsScoreBean ? new GoodsScoreBean() : goodsScoreBean );
        modelMap.put("goodsId", goodsId);
        return "admin/goods/goodsInfo/setScore";
    }

    @RequestMapping(value = "/setScoreData", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage setScoreData(GoodsScoreBean goodsScoreBean, HttpServletRequest request) {
        ResultMessage resultMessage = new ResultMessage();
        goodsScoreService.setGoodsScore(goodsScoreBean);
        resultMessage.setMsg("设置积分配置成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(ModelMap modelMap, @RequestParam(defaultValue = "1") String searchType, HttpServletRequest request) {
        modelMap.put("searchType", searchType);
        List<GoodsTypeBean> goodsTypeBeanList = goodsTypeService.selecRootType(0);
        if (CollectionUtils.isNotEmpty(goodsTypeBeanList)) {
            goodsTypeBeanList.stream().forEach(c -> {
                c.setCateName("|-----" + c.getCateName());
                List<GoodsTypeBean> childList = goodsTypeService.selecRootType(c.getId());
                if (CollectionUtils.isNotEmpty(childList)) {
                    childList.stream().forEach(a -> {
                        a.setCateName("|-----|-----" + a.getCateName());
                    });
                    c.getChilds().addAll(childList);
                }
            });
            modelMap.put("typeList", goodsTypeBeanList);
        }
        String token = BusinessUtil.getCookieToken(request, "token");
        if (!org.springframework.util.StringUtils.isEmpty(token)) {
            UserBean userBean = (UserBean) redisService.get("LOGIN:" + token);
            if (userBean != null && null != userBean.getId()) {
                modelMap.put("userType", userBean.getUserType());
            }
        }
        return "admin/goods/goodsInfo/index";
    }

    @RequestMapping(value = "/selectGoods", method = RequestMethod.GET)
    public String selectGoods(ModelMap modelMap, @RequestParam(defaultValue = "1") String searchType, @RequestParam(defaultValue = "-1") String isSeckill, @RequestParam(defaultValue = "-1") String isBargain) {
        modelMap.put("searchType", searchType);
        modelMap.put("isSeckill", isSeckill);
        modelMap.put("isBargain", isBargain);
        List<GoodsTypeBean> goodsTypeBeanList = goodsTypeService.selecRootType(0);
        if (CollectionUtils.isNotEmpty(goodsTypeBeanList)) {
            goodsTypeBeanList.stream().forEach(c -> {
                c.setCateName("|-----" + c.getCateName());
                List<GoodsTypeBean> childList = goodsTypeService.selecRootType(c.getId());
                if (CollectionUtils.isNotEmpty(childList)) {
                    childList.stream().forEach(a -> {
                        a.setCateName("|-----|-----" + a.getCateName());
                    });
                    c.getChilds().addAll(childList);
                }
            });
            modelMap.put("typeList", goodsTypeBeanList);
        }
        return "admin/goods/goodsInfo/selectGoods";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap modelMap, String id) {
        if (StringUtils.isNotEmpty(id)) {
            modelMap.put("data", JSON.toJSONString(goodsService.detail(Integer.parseInt(id))));
            modelMap.put("id", Integer.parseInt(id));
        }
        List<GoodsTypeBean> goodsTypeBeanList = goodsTypeService.selecRootType(0);
        List<JSONObject> options = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(goodsTypeBeanList)) {
            goodsTypeBeanList.stream().forEach(c -> {
                JSONObject rootObject = new JSONObject();
                rootObject.put("label", "|-----" + c.getCateName());
                rootObject.put("value", c.getId());
                rootObject.put("disabled", 0);
                options.add(rootObject);
                List<GoodsTypeBean> childList = goodsTypeService.selecRootType(c.getId());
                if (CollectionUtils.isNotEmpty(childList)) {
                    childList.stream().forEach(a -> {
                        JSONObject childObject = new JSONObject();
                        childObject.put("label", "|-----|-----" + a.getCateName());
                        childObject.put("value", a.getId());
                        childObject.put("disabled", 1);
                        options.add(childObject);
                    });
                }
            });
            modelMap.put("typeList", JSON.toJSONString(options));
        }
        List<GoodsRuleBean> goodsRuleBeanList = goodsRuleService.getAllRule();
        if (CollectionUtils.isNotEmpty(goodsRuleBeanList)) {
            modelMap.put("ruleList", JSON.toJSONString(goodsRuleBeanList));
        }
        return "admin/goods/goodsInfo/add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage add(@RequestBody GoodsBean goodsBean, HttpServletRequest request) {
        String token = BusinessUtil.getCookieToken(request, "token");
        Integer merId = null;
        if (StringUtils.isNotEmpty(token)) {
            UserBean userBean = (UserBean) redisService.get("LOGIN:" + token);
            if (null != userBean) {
                merId = userBean.getId();
            }
        }
        ResultMessage resultMessage = new ResultMessage();
        goodsService.addGoods(goodsBean, merId);
        resultMessage.setMsg("新增商品成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage modify(@RequestBody GoodsBean goodsBean) {
        ResultMessage resultMessage = new ResultMessage();
        goodsService.updateGoods(goodsBean);
        resultMessage.setMsg("修改商品成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public ResultMessage delete(String id) {
        ResultMessage resultMessage = new ResultMessage();
        goodsService.delete(Integer.parseInt(id));
        resultMessage.setMsg("删除商品成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/setSort", method = RequestMethod.GET)
    @ResponseBody
    public ResultMessage setSort(String id, String sort) {
        ResultMessage resultMessage = new ResultMessage();
        goodsService.setSort(Integer.parseInt(sort), Integer.parseInt(id));
        resultMessage.setMsg("修改排序成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/setIsShow", method = RequestMethod.GET)
    @ResponseBody
    public ResultMessage setIsShow(String id, String isShow) {
        ResultMessage resultMessage = new ResultMessage();
        goodsService.setIsShow(Integer.parseInt(isShow), Integer.parseInt(id));
        resultMessage.setMsg("修改状态成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/generate", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage generate(@RequestBody GenerateBean generateBean, String id, String value,String goodsType) {
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setData(goodsRuleService.generate(generateBean.getAttrs(), id, value,goodsType));
        return resultMessage;
    }

    @RequestMapping(value = "listData", method = RequestMethod.GET)
    @ResponseBody
    public PageInfo<GoodsBean> listData(int limit, int page, GoodsBean goodsBean, HttpServletRequest request) {
        String token = BusinessUtil.getCookieToken(request, "token");
        Integer merId = -1;
        if (StringUtils.isNotEmpty(token)) {
            UserBean userBean = (UserBean) redisService.get("LOGIN:" + token);
            if (null != userBean) {
                merId = userBean.getId();
            }
        }
        if(StringUtils.isNotEmpty(goodsBean.getBuyType())){
            if("1".equals(goodsBean.getBuyType())){
                goodsBean.setIsBevip(1);
            }
            if("2".equals(goodsBean.getBuyType())){
                goodsBean.setIsBepartner(1);
            }
            if("3".equals(goodsBean.getBuyType())){
                goodsBean.setIsScore(1);
            }
        }
        goodsBean.setMerId(merId);
        Page<GoodsBean> pageInfo = new Page<>(page, limit);
        return new PageInfo(goodsService.listGoods(pageInfo, goodsBean));
    }

}
